﻿
namespace EasyNet.FastDFS
{
    using System.IO;

    class UploadStream : IUploadCallback
    {
        public Stream FileStream { get; private set; }

        public long FileSize { get; private set; }


        public UploadStream(Stream fileStream, long fileSize)
        {
            FileStream = fileStream;
            FileSize = fileSize;
        }

        public int Send(Stream stream)
        {
            var remainBytes = FileSize;
            var buff = new byte[256 * 1024];
            int bytes;

            while (remainBytes > 0)
            {
                try
                {
                    if ((bytes = FileStream.Read(buff, 0, remainBytes > buff.Length ? buff.Length : (int)remainBytes)) < 0)
                    {
                        return -1;
                    }
                }
                catch
                {
                    return -1;
                }

                stream.Write(buff, 0, bytes);
                remainBytes -= bytes;
            }

            return 0;
        }
    }
}
